package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.constant.RedisKeyConstants;
import com.farm.kaiping.entity.Building;
import com.farm.kaiping.entity.Pig;
import com.farm.kaiping.entity.Pigpen;
import com.farm.kaiping.po.BuildingPO;
import com.farm.kaiping.po.PigPO;
import com.farm.kaiping.po.PigpenPO;
import com.farm.kaiping.service.BuildingService;
import com.farm.kaiping.service.PigService;
import com.farm.kaiping.service.PigpenLogicService;
import com.farm.kaiping.service.PigpenService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@AllArgsConstructor
public class PigpenLogicServiceImpl implements PigpenLogicService {

    private final BuildingService buildingService;
    private final PigpenService pigpenService;
    private final PigService pigService;
    private final RedisTemplate<String,Object> redisTemplate;

    /**
     * 新增前校验
     *
     * @param pigpenPO
     */
    @Override
    public void checkBeforeSave(PigpenPO pigpenPO) {
        if(ObjectUtil.isEmpty(pigpenPO.getBuildingId())){
            throw new RuntimeException("厂房id为空");
        }
        if(ObjectUtil.isEmpty(pigpenPO.getPigpenNo())){
            throw new RuntimeException("圈舍编号为空");
        }
        Building building = buildingService.getById(pigpenPO.getBuildingId());

        if (ObjectUtil.isEmpty(building)){
            throw new RuntimeException("无效的厂房");
        }

    }

    /**
     * 删除前校验
     * @param pigpenPO
     */
    @Override
    public void checkBeforeRemove(PigpenPO pigpenPO) {
        // 猪只校验
        PigPO pigPO = new PigPO();
        pigPO.setPigpenIdList(pigpenPO.getBatchDel());
        List<Pig> pigList = pigService.getPigList(pigPO);
        if (ObjectUtil.isNotEmpty(pigList)) {
            throw new RuntimeException("该圈舍下存在猪只,请先清除猪只");
        }
    }

    /**
     * 列表
     *
     * @param pigpenPO
     * @return
     */
    @Override
    public List<PigpenPO> getPigpenList(PigpenPO pigpenPO) {

        if (ObjectUtil.isNotEmpty(pigpenPO.getBuildingName())){
            BuildingPO buildingPO = new BuildingPO();
            buildingPO.setBuildingName(pigpenPO.getBuildingName());
            List<Building> buildingList = buildingService.getBuildingList(buildingPO);
            if (buildingList.isEmpty()){
                return new ArrayList<>();
            }
            pigpenPO.setBuildingIdList(buildingList.stream().map(Building::getId).collect(Collectors.toList()));
        }

        List<Pigpen> selectList = pigpenService.getPigpenList(pigpenPO);
        return selectList.stream().map(pigpen -> {
            PigpenPO pigpenVO = BeanUtil.copyProperties(pigpen,PigpenPO.class);

            pigpenVO.setBuildingName(String.valueOf(redisTemplate.opsForValue().get(RedisKeyConstants.BUILDING_NAME+pigpen.getBuildingId())));

            return pigpenVO;
        }).collect(Collectors.toList());
    }
}
